package cn.sang.service.imp;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import cn.sang.dao.CompositionDao;
import cn.sang.dao.OrderDao;
import cn.sang.model.Composition;
import cn.sang.model.Order;
import cn.sang.model.vo.IndexCompositionListVO;
import cn.sang.service.CompositionService;
import cn.sang.util.AjaxResult;
import cn.sang.util.FileUtils;
import cn.sang.util.StrUtils;

@Service
public class CompositionServiceImp implements CompositionService {
	private static Logger log = Logger.getLogger(CompositionServiceImp.class); 
	@Resource
	private CompositionDao compositionDao;
	
	@Resource
	private OrderDao orderDao;
	
	// 首页 展示最新批改作文列表
	public List<IndexCompositionListVO> indexCompositionList() {
		
		List<IndexCompositionListVO> indexCompositionList = compositionDao.indexCompositionList();
		return indexCompositionList;
	}
	
	//上传作文
	public AjaxResult uploadComposition(HttpServletRequest request,
			MultipartFile compositionFile) {
		String teacherId_Str = request.getParameter("teacherId");
		
		HttpSession session = request.getSession();
		
		Object studentId_obj = session.getAttribute("studentId");
		
		if(studentId_obj == null){
			return AjaxResult.errorResult("上传文件失败");
		}else{
			
			Integer teacherId = null;
			// 指定老师
			if(StrUtils.isEmpty(teacherId_Str)){
				return AjaxResult.errorResult("上传文件失败,未指定老师");
			}else{
				teacherId = Integer.valueOf(teacherId_Str);
			}
			
			
			Integer studentId = (Integer) studentId_obj;
			
			Map<String, Object> uploadFile = null;
			try {
				uploadFile = FileUtils.uploadFile(request, compositionFile, "/composition/");
			} catch (IOException e) {
				e.printStackTrace();
				return AjaxResult.errorResult("上传失败");
			}
			
			if(uploadFile != null){
				//作文保留路径
				String jdbcUrl = (String) uploadFile.get("jdbcUrl");
				
				Composition composition = new Composition();
				
				composition.setS_id(studentId);
				composition.setContentUrl(jdbcUrl);
				
				//添加记录到作文表
				try {
					compositionDao.insertComposition(composition);
				} catch (Exception e) {
					e.printStackTrace();
					return AjaxResult.errorResult("上传失败");
				}
				
				Integer c_id = composition.getC_id();
				if(c_id == null){
					//未返回 作文id 
					log.info("===============>>保存作文后 未返回作文id");
					return AjaxResult.errorResult("上传失败");
				}else{
					
					Order order = new Order();
					order.setC_id(c_id);
					order.setS_id(studentId);
					order.setT_id(1);
					order.setcTime(new Date());
					order.setStatus(teacherId);
					order.setPrice(10);
					
					//添加记录到订单表
					try {
						orderDao.insertOrder(order);
					} catch (Exception e) {
						e.printStackTrace();
						log.info("===================>>上传作文 ， 添加到订单表失败");
						return AjaxResult.errorResult("上传失败");
					}
					
					
				}
				
			}
			
			return AjaxResult.successResult("上传成功");
		}	
	}
	
}
